package com.mhkj.revise.professional.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mhkj.base.templete.service.PlanTempleteService;
import com.mhkj.common.enums.PlanType;
import com.mhkj.revise.plan.entity.Plan;
import com.mhkj.revise.professional.service.ProfessionalPlanNodeService;
import com.mhkj.revise.professional.service.ProfessionalPlanService;
import com.mics.core.common.componet.JsonHandler;
import com.mics.core.common.componet.Pagination;
import com.mics.core.common.componet.ResponseMessage;
import com.mics.core.common.exception.ErrorCode;
import com.mics.core.common.exception.ParamException;
import com.mics.core.system.base.controller.BaseController;
import com.mics.core.system.organizational.bean.Employee;
import com.mics.core.system.rbac.entity.Dictionary;
import com.mics.core.system.rbac.service.DictionaryService;

/**
 * 专项计划
 * @author mics
 * @date 2018年8月20日
 * @version 1.0
 */
@Controller
@RequestMapping("/professionalPlan")
public class ProfessionalPlanController extends BaseController<Plan>{
	@Resource
	private ProfessionalPlanService professionalPlanService;
	@Resource
	private ProfessionalPlanNodeService professionalPlanNodeService;
	@Resource
	private JsonHandler jsonHandler;
	@Resource
	private DictionaryService dictionaryService;
	@Resource
	private PlanTempleteService planTempleteService;

	
	@RequestMapping("/listByPage")
	@ResponseBody
	public void listByPage(HttpServletResponse response,Plan plan,int pageIndex,int pageSize){
		Pagination<Plan> pagination =  professionalPlanService.listByPage(plan,pageIndex,pageSize);
		jsonHandler.sendJsonString(response, true, "获取数据成功", pagination, new String[]{
			"id","name","item.name","plan.name","beginDate","endDate","createDate","state","auditDate","auditRemark"}, 
			"id","name","itemName","keyPlanName","beginDate","endDate","createDate","state","auditDate","auditRemark");
	}
	
	
	/**
	 * 保存主项计划
	 * @author mics
	 * @date 2018年8月18日
	 * @version 1.0
	 */
	@RequestMapping("/save")
	@ResponseBody
	public void save(HttpServletResponse response,Plan plan){
		if(plan == null){
			throw new ParamException(ErrorCode.PARAM_ERROR,"参数错误！");
		}
		//保存计划类型
		Dictionary planType = dictionaryService.getByCode(PlanType.PROFESSION_TYPE.getCode());
		plan.setPlanType(planType);
		
		String message =  professionalPlanService.save(plan);
		if(StringUtils.hasText(message)){
			jsonHandler.sendJsonSimple(response, false, message);
		}else{
		    plan = professionalPlanService.getById(plan.getId());
			jsonHandler.sendJsonString(response, true, "获取数据成功", plan, new String[]{"id"}, "planId");
		}	
	}
	
	/**
	 * 推算周期
	 * @author mics
	 * @date 2018年8月13日
	 * @version 1.0
	 */
	@RequestMapping("/calsDealine")
	@ResponseBody
	public ResponseMessage calsDealine(HttpServletResponse response,long planId){
		professionalPlanNodeService.calsDealinePrepare(planId);
		return ResponseMessage.createMessage(true, "推算周期成功");
	}
	
	/**
	 * 
	 * @author mics
	 * @date 2018年8月22日
	 * @version 1.0
	 */
	@RequestMapping("/listAll")
	@ResponseBody
	public ResponseMessage listAll(){
		return null;
	}
	
	
	@RequestMapping("/info")
	@ResponseBody
	public void info(HttpServletResponse response,long id) {
		Plan plan =  professionalPlanService.getById(id);
		jsonHandler.sendJsonString(response, true, "获取数据成功", plan, new String[]{"id","name","planType.value","planTemplete.name"}, "id","name","planType","planTemplete");
	}
}
